<?php

namespace app\api\controller;

use app\admin\model\GoodsOrder;
use app\common\controller\Api;
use app\common\model\User;
use think\Env;

/**
 *  我的好友接口
 */
class Friend extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     *  我的好友列表
     *
     */
    public function index()
    {
        $user_id = $this->auth->id;
        $friend = User::where('id', $user_id)->value('child_tree');
        $friend_ids = explode(',', $friend);
        $data = User::where('id', 'in', $friend_ids)
            ->field('id,nickname,avatar,createtime')
            ->select();
        foreach ($data as &$v) {
            $v['avatar'] = Env::get('app.yuming') . $v['avatar'];
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
        }
        // 好友总数
        $friend_num = count($friend_ids);
        // 拼中总数
        $friend_win = GoodsOrder::where(['user_id' => ['in', $friend_ids], 'type' => 1])->count();
        $this->success('获取成功', $data = ['data' => $data, 'friend_num' => $friend_num, 'friend_win' => $friend_win], 1);
    }
}
